<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm">
                <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item label="字典名称：" prop="classname">
                            <el-input v-model="listQuery.classname"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="字典类型：" prop="kindid">
                            <el-select v-model="listQuery.kindid">
                                <el-option label="产业" value=1 key="1"></el-option>
                                <el-option label="经营" value=2 key="2"></el-option>
                                <el-option label="售房" value=3 key="3"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="状态：" prop="status">
                            <el-select v-model="listQuery.status">
                                <el-option label="正常" :value="0" key="0"></el-option>
                                <el-option label="禁用" :value="1" key="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <div>
                            <el-button size="mini" type="primary" @click="getList">查询</el-button>
                            <el-button size="mini" @click="pageNum = 1;resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">新增数据字典</el-button>
            <el-table v-loading="listLoading" :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="字典编号" prop="classid">
                </el-table-column>

                <el-table-column align="center" label="字典名称" prop="classname">
                </el-table-column>

                <el-table-column align="center" label="字典数值" prop="classdetailvalueisnumber">
                    <template slot-scope="scope">
                        <span>{{ scope.row.classdetailvalueisnumber == 1 ? "是" : "否" }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="状态" prop="status">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status == 0 ? "正常" : "禁用" }}</span>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.classid)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '新增数据字典' : isAdd == 'update' ? '修改数据字典' : '数据字典详情'"
            :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="字典编号：" prop="classid">
                    <el-input v-model="formData.classid" disabled></el-input>
                </el-form-item>
                <el-form-item label="字典名称：" prop="classname">
                    <el-input v-model="formData.classname"></el-input>
                </el-form-item>
                <el-form-item label="字典数值：" prop="classdetailvalueisnumber">
                    <el-select v-model="formData.classdetailvalueisnumber">
                        <el-option label="是" :value="1" key="1"></el-option>

                        <el-option label="否" :value="0" key="0"></el-option>

                    </el-select>
                </el-form-item>

                <el-form-item label="字典类型：" prop="kindid">
                    <el-select v-model="formData.kindid">
                        <el-option label="产业" :value=1 key="1"></el-option>
                        <el-option label="经营" :value=2 key="2"></el-option>
                        <el-option label="售房" :value=3 key="3"></el-option>
                    </el-select>
                </el-form-item>



                <el-form-item label="状态：" prop="status">
                    <el-select v-model="formData.status">
                        <el-option label="正常" :value=0 key="0"></el-option>

                        <el-option label="禁用" :value=1 key="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="备注：" prop="note">
                    <el-input v-model="formData.note" type="textarea" :rows="2"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogVisible = false" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="save" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { listWordBook, createWordBook, updateWordBook, deleteWordBook } from '@/api/system'
import { Message } from 'element-ui'
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 10,
                classname: "",
                kindid: "",
                status: "",
            },
            total: 0,
            list: [

            ],
            listLoading: false,
            dialogVisible: false,
            isAdd: 'add',
            formData: {
                classid: "",
                classname: "",
                classdetailvalueisnumber: "",
                kindid: "",
                note: "",
                status: "",

            },
        };
    },

    computed: {
        deptName() {
            return (val) => {
                if (val == "1") {
                    return "事业部";
                }
                return "生活部";
            };
        },
    },

    mounted() {
        this.getList();
    },

    methods: {

        save() {
            if (this.isAdd == 'add') {
                createWordBook({ ...this.formData }).then(res => {
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 3 * 1000,
                    });
                    this.dialogVisible = false;
                    this.getList();
                })
            }

            if (this.isAdd = 'update') {
                updateWordBook({ ...this.formData }).then(res => {
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 3 * 1000,
                    });
                    this.dialogVisible = false;
                    this.getList();
                })
            }
        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {

            listWordBook(this.listQuery).then((response) => {
                this.total = response.data.total;
                this.list = response.data.list;

            });
        },

        resetForm(val) {

            this.$refs[val].resetFields();
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
        },

        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.formData = { ...currentData };
            });
        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteWordBook(id).then(res => {
                        Message({
                            message: res.message || '操作成功',
                            type: "success",
                            duration: 3 * 1000,
                        });
                        this.getList();
                    })
                })
                .catch(() => { });
        }
    },
};
</script>